package com.hdax.dm.order.mq;

import com.hdax.dm.entity.order.Order;
import com.hdax.dm.order.feign.SchedulerSeatFeign;
import com.hdax.dm.order.service.OrderLinkUserService;
import com.hdax.dm.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 监听器
 */
@RabbitListener(queues = {"linkuser_rollback","order_rollback","seat_rollback","dm_dlx_queue"})
@Component
public class OrderMqListener {

    @Autowired
    private OrderLinkUserService userService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private SchedulerSeatFeign schedulerSeatFeign;

    //订单联系人新增失败的数据回滚
    //channel意味频道
    @RabbitHandler
    public void rollbackLinkUser(Channel channel, Message message, String orderNo) throws IOException {

        String queue= message.getMessageProperties().getConsumerQueue();
        Long tag=message.getMessageProperties().getDeliveryTag();
        //删除
        if (queue.equals("linkuser_rollback")){
            boolean flag=userService.deleteByOrderNo(orderNo);
            //如果为真的话,手动判断
            if (flag){
                channel.basicAck(tag,false);
            }else {
                //三个参数  分别传入,唯一标识(tag)  是否批量  是否重回队列
                channel.basicNack(tag,false,false);
                //quartz 定时  发送3次
                //如果仍然无法消费  日志记录,人工干预
            }
        }else if (queue.equals("order_rollback")){
            boolean flag = orderService.deleteOrder(orderNo);
            if (flag) {
                channel.basicAck(tag,false);
            }else {
                channel.basicNack(tag,false,false);
            }
        }else if (queue.equals("seat_rollback")){
            boolean flag = schedulerSeatFeign.seatRollback(orderNo);
            if (flag) {
                channel.basicAck(tag,false);
            }else {
                channel.basicNack(tag,false,false);
            }
        }else  if (queue.equals("dm_dlx_queue")){
            Order order =orderService.order(orderNo);
            if(order.getOrderType()==2){
                System.out.println(orderNo+ "订单已经支付完成");
                //
                channel.basicAck(tag,false);
            }else {
                System.out.println(orderNo + "订单超时未支付！");
                boolean a = orderService.updateOrderChao(orderNo);
                boolean flag = schedulerSeatFeign.seatRollback(orderNo);
                if (a && flag) {
                    channel.basicAck(tag, false);
                } else {
                    //basicNack(tag,是否批量,是否重回队列)
                    channel.basicNack(tag, false, false);
                    //quartz 定时 发送三次
                    //依然无法消费，日志记录，人工干预
                }
            }
        }

    }

}
